﻿namespace DeXrm.Win.Trace
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Clas.Trace;
    using Clas.Trace.Object;
    using Properties;

    public partial class DeXrmTraceSett : Form
    {
        public DeXrmTraceSett()
        {
            InitializeComponent();

            #region Eventos
            btnOpenFolder.Click += BtnOpenFolderClick;
            chbTrace.CheckedChanged += ChbTraceCheckedChanged;
            btnAceptar.Click += btnAceptar_Click;
            #endregion
        }

        #region Eventos
        void ChbTraceCheckedChanged(object sender, EventArgs e)
        {
            if (chbTrace.Checked)
            {
                #region Confgurar Objeto de Trace
                nIconTrace.Visible = true;

                var objectRegedit = new DeXrmObjectRegedit
                                        {
                                            TraceCategories = "*:Verbose",
                                            TraceDirectory = txtFolderTrace.Text,
                                            TraceEnabled = true
                                        };
                new DeXrmCrmRegedit().ChangeRegedit(objectRegedit);
                #endregion
            }
            else
            {
                #region Confgurar Objeto de Trace

                var objectRegedit = new DeXrmObjectRegedit
                                        {
                                            TraceEnabled = false
                                        };
                new DeXrmCrmRegedit().ChangeRegedit(objectRegedit);
                nIconTrace.Visible = false;
                #endregion
            }
        }

        void BtnOpenFolderClick(object sender, EventArgs e)
        {
            fbDialog.ShowNewFolderButton = true;
            fbDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            DialogResult result = fbDialog.ShowDialog();
            if (result == DialogResult.OK)
                txtFolderTrace.Text = fbDialog.SelectedPath;
        }

        void btnAceptar_Click(object sender, EventArgs e)
        {
            new DeXrmTrace().VerificaEnabledTrace(chbTrace.Checked, this.txtFolderTrace.Text);
            this.Close();
        }

        protected override void OnFormClosing(FormClosingEventArgs formClosingEventArgs)
        {
            new DeXrmTrace().VerificaEnabledTrace(chbTrace.Checked, this.txtFolderTrace.Text);
        } 
        #endregion

        private void DeXrmTraceSettLoad(object sender, EventArgs e)
        {
            #region Notification

            nIconTrace.Text = Resources.IconText;
            nIconTrace.BalloonTipText = Resources.IconText;
            nIconTrace.BalloonTipTitle = Resources.TipTitle;
            nIconTrace.ShowBalloonTip(1000);

            #endregion

            chbTrace.Checked = DeXrmCrmRegedit.GetValueOption("TraceEnabled", "Software\\Microsoft\\MSCRM");

            txtFolderTrace.Text = String.Format("{0}\\{1}",
                DeXrmCrmRegedit.GetValueString("CRM_Server_InstallDir", "Software\\Microsoft\\MSCRM")
                ,"Trace");
        }

    }
}
